package com.yvdelx.cyclowomen.activity;

import android.app.Activity;
import android.os.Bundle;
import android.view.Menu;
import android.view.MenuItem;
import android.widget.LinearLayout;

import com.jjoe64.graphview.GraphView.GraphViewData;
import com.jjoe64.graphview.GraphView.LegendAlign;
import com.jjoe64.graphview.GraphViewSeries;
import com.jjoe64.graphview.GraphViewSeries.GraphViewSeriesStyle;
import com.yvdelx.R;
import com.yvdelx.cyclowomen.activity.adapter.GraphLabelFormatterAdapter;
import com.yvdelx.cyclowomen.activity.adapter.GraphViewSeriesStyleAdapter;
import com.yvdelx.cyclowomen.activity.adapter.LineGraphViewAdapter;
import com.yvdelx.cyclowomen.mediation.CycleManagement;
import com.yvdelx.cyclowomen.utility.ActivityUtility;
import com.yvdelx.cyclowomen.utility.MathUtility;
import com.yvdelx.cyclowomen.utility.StringUtility;

import java.util.List;

import static android.graphics.Paint.Align;

/**
 * Graph activity displays the graphic of the current cycle
 */
public class GraphActivity extends Activity {

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_graph);

        ActivityUtility.addActivity(this);

        // Initialize Graph
        LineGraphViewAdapter graphView = new LineGraphViewAdapter(this, " ");
        graphView.setCustomLabelFormatter(new GraphLabelFormatterAdapter(this));

        // Get datas to draw the graph
        List<GraphViewData[]> parameters = CycleManagement.getDaysGraphViewData();

        // Initialize and add series to the graph
        GraphViewData[] dayData = parameters.get(0);
        GraphViewSeries daySeries = new GraphViewSeries(
                StringUtility.getStringResource(R.string.graph_day_title, this),
                getGraphViewSeriesStyle(R.color.blue, true),
                dayData);
        graphView.addSeries(daySeries);

        GraphViewData[] averageLowTemp = parameters.get(1);
        GraphViewSeries averageLowTempSeries = new GraphViewSeries(
                StringUtility.getStringResource(R.string.graph_average_low_title, this),
                getGraphViewSeriesStyle(R.color.red, false),
                averageLowTemp);
        graphView.addSeries(averageLowTempSeries);

        GraphViewData[] averageHighTemp = parameters.get(2);
        if (!MathUtility.isAverageNull(averageHighTemp)) {
            GraphViewSeries averageHighTempSeries = new GraphViewSeries(
                    StringUtility.getStringResource(R.string.graph_average_high_title, this),
                    getGraphViewSeriesStyle(R.color.lightgreen, false),
                    averageHighTemp);
            graphView.addSeries(averageHighTempSeries);
        }

        // Put settings in the graph
        graphView.setDrawDataPoints(true);
        graphView.setDataPointsRadius(15f);

        graphView.getGraphViewStyle().setNumVerticalLabels(5);
        graphView.getGraphViewStyle().setNumHorizontalLabels(dayData.length);
        graphView.getGraphViewStyle().setVerticalLabelsAlign(Align.CENTER);
        graphView.getGraphViewStyle().useTextColorFromTheme(this);
        graphView.getGraphViewStyle().setGridColor(getResources().getColor(R.color.gray));

        graphView.setShowLegend(true);
        graphView.setLegendAlign(LegendAlign.BOTTOM);
        graphView.setScalable(true);
        graphView.getGraphViewStyle().setLegendWidth(400);

        // Draw the layout
        LinearLayout layout = (LinearLayout) findViewById(R.id.layout_graph);
        layout.addView(graphView);

        this.setTitle(getResources().getString(R.string.graph_title));
    }

    /**
     * return the style series of graphics
     *
     * @param indexColor            Integer
     * @param drawTextOnPointRadius Boolean
     * @return GraphViewSeriesStyle
     */
    private GraphViewSeriesStyle getGraphViewSeriesStyle(final Integer indexColor, final Boolean drawTextOnPointRadius) {
        GraphViewSeriesStyleAdapter graphViewSeriesStyleAdapter = new GraphViewSeriesStyleAdapter(
                getResources().getColor(indexColor),
                3);

        graphViewSeriesStyleAdapter.setDrawValueXOnPoint(drawTextOnPointRadius);

        return graphViewSeriesStyleAdapter;
    }

    @Override
    public boolean onCreateOptionsMenu(Menu menu) {
        // Inflate the menu; this adds items to the action bar if it is present.
        getMenuInflater().inflate(R.menu.graph, menu);
        return true;
    }

    @Override
    public boolean onOptionsItemSelected(MenuItem item) {
        // Handle action bar item clicks here. The action bar will
        // automatically handle clicks on the Home/Up button, so long
        // as you specify a parent activity in AndroidManifest.xml.
        int id = item.getItemId();
        return id == R.id.action_settings || super.onOptionsItemSelected(item);
    }

    @Override
    public void onBackPressed() {
        ActivityUtility.closeActivity(this);
    }
}
